﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CGOnline.Server.Components;
using System.Runtime.Serialization;
using System.IO;
using System.Xml;
using CGOnline.Server.Core;

namespace CGOnline.Server.UserLayer
{
    public class PlatformComponent:ServerComponent
    {
        List<UserBase> _userlist;
        List<RoomBase> _roomlist;
        AccessRestrictions _accessrestrictions;

        public PlatformComponent()
            : base()
        {
            _userlist = new List<UserBase>();
            _roomlist = new List<RoomBase>();
            _accessrestrictions =new XmlAccessRestrictions(UserLayerSettings.Default.ARFilePath);
        }

        public UserBase ConnectUser(IUserChannel userchannel)
        {
            var user = _userlist.First(u => u.Config.UserIdentity.CompareTo(userchannel.GetUserIdentity()) == 0);
            if (user != null)
                user.UserChannel = userchannel;
            else
            {
                UserConfig config = new UserConfig(_accessrestrictions);
                config.Merge(userchannel.GetUserConfig());
                config.GoToState(ConfigBase.CONFIG_STATEGROUP, ConfigBase.CONFIG_STATEGROUP_INSTANCE);
                user = new CGUser(userchannel,config);
            }
            user.GoToState(UserBase.USER_STATEGROUP, UserBase.USER_STATEGROUP_ONLINE);
            return user;
        }

        public bool DisconnectUser(Identity identity)
        {
            var user = _userlist.First(u => u.Config.UserIdentity.CompareTo(identity) == 0);
            if (user == null)
                return false;
            user.GoToState(UserBase.USER_STATEGROUP,UserBase.USER_STATEGROUP_OFFLINE);
            user.UserChannel = null;
            return true;
        }

        public RoomBase CreateRoom(Identity identity,RoomConfig config2)
        {
            RoomConfig config = new RoomConfig(_accessrestrictions);
            config.Merge(config2);
            config.GoToState(ConfigBase.CONFIG_STATEGROUP, ConfigBase.CONFIG_STATEGROUP_INSTANCE);
            var room = new Room(config);
            return room;
        }

        public List<RoomConfig> ListRoom(Func<RoomBase,bool> filter)
        {
            var filterlist = _roomlist.Where(filter);
            var roomcfglist = new List<RoomConfig>();
            foreach (var room in filterlist)
                roomcfglist.Add(room.Config.Copy() as RoomConfig);
            return roomcfglist;
        }
    }

    //public class SettingSerializeHelper
    //{
    //    static public T LoadData<T>(string objpath)
    //    {
    //        var serializer = new DataContractSerializer(typeof(T));
    //        /////////////possible error
    //        object ret = serializer.ReadObject(new XmlTextReader(objpath), false);
    //        return (T)ret;
    //    }
    //    static public bool SaveData<T>(string objpath,T obj)
    //    {
    //        var serializer = new DataContractSerializer(typeof(T));
    //        try
    //        {
    //            serializer.WriteObject(new XmlTextWriter(objpath, Encoding.Unicode), obj);
    //        }
    //        catch (Exception ex)
    //        {
    //            throw new Exception("exception occured while saving setting data", ex);
    //        }
    //        return true;
    //    }
    //}
}
